<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Phone extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(1);
		$this->load->model('Candidate_phone_model', 'phone');
		$this->lang->load('candidate/phone', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$phone_list = $this->phone->get(array('idcandidate' => $this->session->userdata['idprofile']));
		$grid_phone = array();
        
        foreach ($phone_list->result() as $row)
		{
			$grid_phone[] = '<div class="row"><span>(' . $row->region . ') ' . $row->number .
			'</span><div class="gtool"><a onclick="CandidatePhone.editPhone('.$row->id.');">'.$this->lang->line('global_update').'</a>
			 &middot; <a onclick="CandidatePhone.deletePhone('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
		}
		
		if (!$grid_phone) {
			 $data['grid_phone'] = '<center>'.$this->lang->line('phone_noresults').'</center>';
		} else {
			 $data['grid_phone'] = implode('<hr/>', $grid_phone);
		}
		
		exitHTML($data['grid_phone']);
	}
	
	public function insert()
	{
		$data = $_POST;
		
		if (missing_field($data, array('region', 'number')))
		{
			formError($this->lang->line('phone_missingfield'), true);
		}
		
		if ($data['country'] == '') {
			$data['country'] = '55';
		}
		
		$data['idcandidate'] = $this->session->userdata['idprofile'];
		
		if ($this->phone->get($data)->row())
		{
			formError($this->lang->line('phone_numberexists'), true);
		}
		
		if ($this->phone->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidatePhone.getPhoneGrid();closeBox();');
		}
	}
	
	public function update($id_phone = '')
	{
		$data = $_POST;
		$data['id_phone'] = $id_phone;
		
		if (missing_field($data, array('region', 'number')))
		{
			formError($this->lang->line('phone_missingfield'), true);
		}
		
		if ($data['country'] == '') {
			$data['country'] = '55';
		}
		
		if ($this->phone->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidatePhone.getPhoneGrid();closeBox();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError('Voc&ecirc; deve preencher todos os campos.');
		}
		
		if ($this->phone->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CandidatePhone.getPhoneGrid();');
		}
	}
	
	public function edit($id_phone = '')
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		$data['id_phone'] = $id_phone;
		
		$data['country'] = '';
		$data['region'] = '';
		$data['number'] = '';
				
		if (is_numeric($id_phone))
		{
			$phone_data = $this->phone->get(array('idphone' => $id_phone))->row();
			$data['country'] = $phone_data->country;
			$data['region'] = $phone_data->region;
			$data['number'] = $phone_data->number;
		}
				
		$data['userdata'] = $this->session->userdata;
        $this->load->view('candidate/phone', $data);
	}
	
	public function add()
	{
		$this->edit();
	}
}

?>